#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/init.h> /* Needed for the macros */
#include <linux/kernel.h> /* Needed for pr_info() */
#include <linux/module.h> /* Needed by all modules */
#include <asm/uaccess.h>
#include <linux/ioport.h>
#include <asm/io.h>
#include <linux/interrupt.h>


//Information and statistics about system interrupts can be found in /proc/interrupts or /proc/stat.
//Only system interrupts with associated interrupt handlers appear in /proc/interrupts:

//RFC-1589

//https://linux-kernel-labs.github.io/refs/heads/master/labs/interrupts.html#interrupt-handling
//All of the port requests can be seen from userspace via the /proc/ioports file:

//sudo mknod /dev/mycdev001 c  403 0






#define MY_MAJOR       404
#define MY_MAX_MINORS  1

#define MY_BASEPORT 0x3F8
#define MY_NR_PORTS 8


irqreturn_t my_handler(int irq_no, void *dev_id)
{
    struct my_device_data *my_data = (struct my_device_data *) dev_id;

    /* if interrupt is not for this device (shared interrupts) */
        /* return IRQ_NONE;*/

    /* clear interrupt-pending bit */
    /* read from device or write to device*/

    return IRQ_HANDLED;
}



struct my_device_data {
	struct cdev cdev;
//...
};

#define MY_BASEPORT 0x3F8
#define MY_IRQ 4

struct my_device_data devs[MY_MAX_MINORS];

struct my_device_data *my_data;

static int my_open(struct inode *inode, struct file *file) {
//	struct my_device_data *my_data;
	pr_info("a3 my_open\n");
//	my_data = container_of(inode->i_cdev, struct my_device_data, cdev);
//	file->private_data = my_data;

	     int err;

	     err = request_irq(MY_IRQ, my_handler, IRQF_SHARED,
	                       "com1", my_data);
	     if (err < 0) {
	         /* handle error*/
	         return err;
	     }
	return 0;
	//...
}

static int my_release(struct inode *inode, struct file *file) {
//	struct my_device_data *my_data;
	pr_info("a3 my_release\n");
//	my_data = container_of(inode->i_cdev, struct my_device_data, cdev);
//	file->private_data = my_data;
	free_irq (MY_IRQ, my_data);

	return 0;
	//...
}




static int r = 0;
static unsigned char mem[100];

static ssize_t my_write(struct file *filp,
		const char __user *user_buffer,
		size_t size,
		loff_t *offset)
{
	pr_info("write");
	int my_size=100;
	ssize_t len = min(my_size - *offset, size);
	if (len <= 0)
	return 0;
//
	/* read data from user buffer to my_data->buffer */
	if (copy_from_user(mem + *offset, user_buffer, len))
	return -EFAULT;
	*offset += len;
	pr_info("r: %s",mem);
	return len;
}

static ssize_t my_read(struct file *filp,
		char __user *buffer,
		size_t count,
		loff_t *offset)
{
	pr_info("a3 my_read: %d \n",r++);
	pr_info("length of the buffer : %d \n",count);
	pr_info("offset : %d \n",*offset);
	uint8_t *data = "Hello123\n";
	size_t datalen = strlen(data);
	int p = *offset;
	if(p > datalen) {
		return 0;
	}
	if (count > datalen -p ) {
		count = datalen-p;
	}
	if (copy_to_user(buffer, data+*offset, count)) {
		return -EFAULT;
	}
	*offset += count;
	return count;
}

const struct file_operations my_fops = {
//		.owner = THIS_MODULE,
		.open = my_open, .read = my_read, .write = my_write,
		.release = my_release,
//		.unlocked_ioctl = my_ioctl
		};

static __init int hello_2_init(void) {
	int err;
	int i;

	unsigned long j, k;

	j = jiffies;

	k =j+ HZ;

	if(time_after(k,j)){
		pr_info("k after j");
	} else {
		pr_info("j after k");
	}



	pr_info("a3 jiffies:%ul\n",j);

	pr_info("a3 HZ:%ul\n",HZ);


	if (!request_region(MY_BASEPORT, MY_NR_PORTS, "com1")) {
		/* handle error */
//	     return -ENODEV;
		pr_info("a3 request_region fail \n");
	} else {
		pr_info("a3 request_region ok \n");
	}

	pr_info("a3 init_module 1111  \n");
	err = register_chrdev_region(MKDEV(MY_MAJOR, 0), MY_MAX_MINORS,
			"my_device_driver");
	if (err != 0) {
		/* report error */
		return err;
	}

	for (i = 0; i < MY_MAX_MINORS; i++) {
		/* initialize devs[i] fields */
		cdev_init(&devs[i].cdev, &my_fops);
		cdev_add(&devs[i].cdev, MKDEV(MY_MAJOR, i), 1);
	}

	return 0;
}

static void __exit hello_2_exit(void) {
	int i;
	pr_info("a3 cleanup_module\n");
	release_region(MY_BASEPORT, MY_NR_PORTS);

	for (i = 0; i < MY_MAX_MINORS; i++) {
		/* release devs[i] fields */
		cdev_del(&devs[i].cdev);
	}
	unregister_chrdev_region(MKDEV(MY_MAJOR, 0), MY_MAX_MINORS);
}

module_init( hello_2_init);
module_exit( hello_2_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Andy");
MODULE_DESCRIPTION("A sample c  driver");
